﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using FrmCommon;
using PartsRetrospect.Business;
using PartsRetrospect.Model;
using PartsRetrospect.Comm;

namespace PartsRetrospect.Forms
{
    public partial class FrmBarCodeEnter : FrmBarCodeEnterBase
    {
        PartsBLL partsBLL = PartsBLL.getInstance();

        bool isLoad = true;      //判断窗体是否首次加载

        public FrmBarCodeEnter()
        {
            InitializeComponent();

            BodyNoLength = 5;
            VinCodeLength = 14;
            ManyNoLength = 4;
            PartsNoLength = 14;

            GolbalVariable.BarCode = new BindingBarCodeModel();
        }

        private void FrmBarCodeEnter_Load(object sender, EventArgs e)
        {
            //如果窗体已经加载过则不重新加载
            if (!isLoad)
                return;
            isLoad = false;

            GolbalVariable.BarCode.ShowMessageBox = new BindingBarCodeModel.ShowMessageDelete(this.ShowMessage);

            //partsBLL.OnCarBarCodeReceived += CarBarCode_ReceivedEvent;
            //partsBLL.OnPartBarCodeReceived += PartBarCode_ReceivedEvent;
            txtBodyNo.DataBindings.Add(new Binding("Text", GolbalVariable.BarCode, "BodyNo"));
            txtVinCode.DataBindings.Add(new Binding("Text", GolbalVariable.BarCode, "VinCode"));
            txtPartsNo.DataBindings.Add(new Binding("Text", GolbalVariable.BarCode, "PartsNo"));
            txtManyNo.DataBindings.Add(new Binding("Text", GolbalVariable.BarCode, "ManyNo"));

            //partsBLL.pm.PropertyChanged += eventTest;
        }

        private bool ShowMessage(string str)
        {
            UIMessageBox.Show(str);
            return true;
        }

        public override void btnHandSure_Click(object sender, EventArgs e)
        {

            txtBodyNo.FillColor = Color.White;
            txtVinCode.FillColor = Color.White;
            txtPartsNo.FillColor = Color.White;
            txtManyNo.FillColor = Color.White;
            string vin = txtVinCode.Text.Trim();
            string BodyNo = txtBodyNo.Text.Trim();
            string ManyNo = txtManyNo.Text.Trim();
            string parts = txtPartsNo.Text.Trim();

            if (BodyNo.Length != BodyNoLength)
            {
                UIMessageBox.ShowError("BODY NO输入未完成，请重新检查!");
                txtBodyNo.FillColor = Color.Red;
                return;
            }

            if (vin.Length != VinCodeLength)
            {
                UIMessageBox.ShowError("VIN NO输入未完成，请重新检查。");
                txtVinCode.FillColor = Color.Red;
                return;
            }
            if (ManyNo.Length != ManyNoLength)
            {
                UIMessageBox.ShowError("连番号信息输入未完成，请重新检查。");
                txtManyNo.FillColor = Color.Red;
                return;
            }

            if (parts.Length != PartsNoLength)
            {
                UIMessageBox.ShowError("部品信息输入未完成，请重新检查。");
                txtPartsNo.FillColor = Color.Red;
                return;
            }

            string mes = partsBLL.VinCodeCheck(txtVinCode.Text);
            if(mes != "")
            {
                UIMessageBox.ShowError(mes);
                return;
            }
            mes = partsBLL.BarEnter(txtPartsNo.Text);
            if (mes != "")
            {
                UIMessageBox.ShowError(mes);
                return;
            }
            else
            {
                UIMessageBox.ShowInfo("上传成功！");
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            UILight light = new UILight();
            //light.Name = "light" + 1;
            //light.Text = "";
            light.Size = new System.Drawing.Size(25, 25);
            light.OnColor = Color.Green;
            light.OffColor = Color.Red;
            light.State = UILightState.Blink;
            //light.FillColor = Color.FromArgb(128, 255, 128);
            UILabel label = new UILabel();
            label.Text = "IO卡     ";
            label.Height = 30;
            //建议用封装的方法Add
            uiFlowLayoutPanel1.Add(light);
            uiFlowLayoutPanel1.Add(label);
        }

        private void CarBarCode_ReceivedEvent(BindingBarCodeModel model)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                txtBodyNo.Text = model.BodyNo;
                txtVinCode.Text = model.VinCode;
                txtManyNo.Text = model.ManyNo;
            }));
        }

        private void PartBarCode_ReceivedEvent(BindingBarCodeModel model)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                txtPartsNo.Text = model.PartsNo;
            }));
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
        }
    }
}
